package com.yyr.callmessage.user.domain.service;

import com.yyr.callmessage.user.domain.cache.VerificationCache;
import com.yyr.callmessage.user.domain.entity.User;
import com.yyr.callmessage.user.domain.reposity.UserRepository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Description:
 * @Author：
 * @Date：2021/6/25
 */
@Service
public class UserService {

    @Resource
    private UserRepository userRepository;

    @Resource
    private VerificationCache cache;

    public User get(Long id){
        return userRepository.findById(id).orElse(null);
    }

    public User getByPhone(String phone){
        return userRepository.findByPhone(phone);
    }

    public void add(User user){
        userRepository.save(user);
    }



    /**
     * 添加 SMS：phone - value 短信验证码缓存
     * @param phone
     * @param verificationSMS
     */
    public void addVerificationSMS(String phone , String verificationSMS){
        cache.addVerificationSMS(phone , verificationSMS);
    }

    /**
     * 获取短信验证码缓存
     * @param phone
     * @return
     */
    public String getVerificationSMS(String phone){
        return cache.getVerificationSMS(phone);
    }

    /**
     * 生成四位随机码
     * @return
     */
    public String getRandomNumber(){
        int num = (int) ((Math.random()*9+1) * 1000);
        return String.valueOf(num);
    }

}
